package cc.ucanuup.web.base.controller;

import java.util.ArrayList;
import java.util.List;

import javax.annotation.Resource;

import org.springframework.data.domain.Page;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import cc.ucanuup.common.support.base.Msg;
import cc.ucanuup.common.support.easyui.model.SearchComgrid;
import cc.ucanuup.common.support.easyui.view.EasyuiPage;
import cc.ucanuup.common.support.easyui.view.EasyuiTree;
import cc.ucanuup.service.base.entity.RelationUserRole;
import cc.ucanuup.service.base.entity.UpRole;
import cc.ucanuup.service.base.entity.UpUser;
import cc.ucanuup.service.base.inter.UpRoleService;
import cc.ucanuup.service.base.inter.UpUserService;
import cc.ucanuup.service.base.inter.UserRoleService;
import cc.ucanuup.utils.StringUtil;
import cc.ucanuup.web.base.model.SearchUserModel;
import cc.ucanuup.web.base.model.UserPowerModel;

/**
 * Description: TODO[描述该类概要功能介绍]
 *
 * @author guwenbin
 * @date 2018年2月3日
 * @version 1.0
 */
@Controller
@RequestMapping("/srm/supplier")
public class UpUserController {

	@Resource
	private  UpUserService  upUserService;
	@Resource
	private  UpRoleService upRoleService;
	@Resource
	private UserRoleService userRoleService;

	/**
	 * @Method: com.bozhon.web.base.controller.UserController.menuPage
	 * @Description: 用户页面
	 * @author: WenBin
	 * @date: 2017年10月14日
	 * @version: 1.0
	 * @return
	 * String
	 * @update [日期YYYY-MM-DD] [更改人姓名][变更描述]
	 */
	@RequestMapping("/userPage")
	//@RequiresPermissions(value = "user:userPage")
	public String menuPage(){
		return "base/power/user/userPage";
	}

	/**
	 * @Method: com.bozhon.web.base.controller.UserController.addPowerPage
	 * @Description: 添加权限页面
	 * @author: WenBin
	 * @date: 2017年10月17日
	 * @version: 1.0
	 * @return
	 * String
	 * @update [日期YYYY-MM-DD] [更改人姓名][变更描述]
	 */
	// TODO
	@RequestMapping("/addPowerPage")
	//@RequiresPermissions(value = "user:userPage")
	public String addPowerPage(){
		return "base/power/user/addPower";
	}

	/**
	 * @Method: com.bozhon.web.base.controller.UserController.editUserColumnPowerPage
	 * @Description: 编辑用户字段权限的页面
	 * @author: WenBin
	 * @date: 2018年1月18日
	 * @version: 1.0
	 * @param id
	 * @param userId
	 * @param model
	 * @return
	 * String
	 * @update [日期YYYY-MM-DD] [更改人姓名][变更描述]
	 */
	// TODO
	@RequestMapping("/editUserColumnPowerPage")
	//@RequiresPermissions(value = "user:userPage")
	public String editUserColumnPowerPage(String id,String userId,Model model){
		/*UserPower up = userPowerService.findOne(id == null ? "" :id);
		if(up==null){
			up = new UserPower();
		}
		List<MasterCodeDto> list = masterCodeService.findActiveMasterCodeDto(MasterCodeType.USERPOWER_SHOWTYPE);
		model.addAttribute("userPower", up);
		model.addAttribute("userId", userId);
		model.addAttribute("userPowerType", list);*/
		return "base/power/user/editUserColumnPowerPage";
	}

	/**
	 * @Method: com.bozhon.web.base.controller.UserController.saveOrUpdateUserColumnPower
	 * @Description: 保存或者更新用户权限
	 * @author: WenBin
	 * @date: 2017年12月2日
	 * @version: 1.0
	 * @param model
	 * @return
	 * Object
	 * @update [日期YYYY-MM-DD] [更改人姓名][变更描述]
	 */
	// TODO
	@RequestMapping(value="/saveOrUpdateUserColumnPower")
	@ResponseBody
	public Object saveOrUpdateUserColumnPower(UserPowerModel model){
		/*UserPower userPower = userPowerService.findOne(model.getId()==null? "":model.getId());
		if(userPower == null){
			userPower = new UserPower();
			userPower.setColumnName(model.getColumnName());
			userPower.setShowType(model.getShowType());
			userPower.setTableName(model.getTableName());
			userPower.setUserId(model.getUserId());
			userPowerService.save(userPower);
		}else{
			userPower.setColumnName(model.getColumnName());
			userPower.setShowType(model.getShowType());
			userPower.setTableName(model.getTableName());
			userPower.setUserId(model.getUserId());
			userPower.setUpdatedBy(AppUtil.getUserInfoContext().getUserId());
			userPowerService.save(userPower);
		}*/
		return new Msg();
	}

	/**
	 * @Method: com.bozhon.web.base.controller.UserController.delUserColumnPower
	 * @Description: 删除用户字段权限
	 * @author: WenBin
	 * @date: 2017年12月2日
	 * @version: 1.0
	 * @param id 用户权限 主键
	 * @return
	 * Object
	 * @update [日期YYYY-MM-DD] [更改人姓名][变更描述]
	 */
	// TODO
	@RequestMapping(value="/delUserColumnPower")
	@ResponseBody
	public Object delUserColumnPower(String id){
		if(StringUtil.isEmpty(id)){
			return new Msg(false,"50","角色ID 或者用户ID不存在！");
		}
		synchronized (this) {
			/*UserPower up = userPowerService.findOne(id);
			if(up != null){
				up.setDeleted(1);
				userPowerService.save(up);
			}*/
		}
		return new Msg();
	}

	/**
	 * @Method: com.bozhon.web.base.controller.UserController.queryUser
	 * @Description: 查询用户分页信息
	 * @author: WenBin
	 * @date: 2017年10月17日
	 * @version: 1.0
	 * @param searchUserModel
	 * @return
	 * Object
	 * @throws Exception
	 * @update [日期YYYY-MM-DD] [更改人姓名][变更描述]
	 */
	@RequestMapping(value="/queryUser")
	@ResponseBody
	public Object queryUser(SearchUserModel searchUserModel) throws Exception{

		return upUserService.queryUserDtoAndSortIt(searchUserModel, searchUserModel.getUserMsg());
	}

	/**
	 * @Method: com.bozhon.web.base.controller.UserController.queryPowerTree
	 * @Description: 查询权限的树分支（树）
	 * @author: WenBin
	 * @date: 2017年10月17日
	 * @version: 1.0
	 * @param searchUserModel
	 * @return
	 * Object
	 * @update [日期YYYY-MM-DD] [更改人姓名][变更描述]
	 */
	// TODO
	@RequestMapping(value="/queryPowerTree")
	@ResponseBody
	public Object queryPowerTree(String userId){
		/*	return roleService.findRoleTreeListByUserId(userId);*/
		return upRoleService.findRoleTreeListByUserId(userId);
	}

	/**
	 * @Method: com.bozhon.web.base.controller.UserController.queryDefaultPowerTree
	 * @Description: 查询供应商的默认权限树
	 * @author: WenBin
	 * @date: 2018年1月18日
	 * @version: 1.0
	 * @param userId
	 * @return
	 * Object
	 * @update [日期YYYY-MM-DD] [更改人姓名][变更描述]
	 */
	// TODO
	@RequestMapping(value="/queryDefaultPowerTree")
	@ResponseBody
	public Object queryDefaultPowerTree(String userId){
		UpUser sp = upUserService.findSupplierByUserId(userId);
		List<UpRole> list = null;
		if(sp!=null){
			list = upRoleService.findDefaultRoleWithType(sp.getUserType());
		}
		List<EasyuiTree> rt = new ArrayList<EasyuiTree>();
		EasyuiTree tree = null;
		for(UpRole role : list){
			tree = new EasyuiTree();
			tree.setId(role.getId());
			tree.setText(role.getRoleName());
			rt.add(tree);
		}
		return rt;
	}

	/**
	 * @Method: com.bozhon.web.base.controller.UserController.queryUserColumnPowerTree
	 * @Description: 查询用户字段权限树
	 * @author: WenBin
	 * @date: 2018年1月18日
	 * @version: 1.0
	 * @param userId
	 * @return
	 * Object
	 * @update [日期YYYY-MM-DD] [更改人姓名][变更描述]
	 */
	// TODO
	@RequestMapping(value="/queryUserColumnPowerTree")
	@ResponseBody
	public Object queryUserColumnPowerTree(String userId){
		//return userPowerService.findUserPowerTreeListByUserId(userId);
		return null;
	}
	/**
	 * @Method: com.bozhon.web.base.controller.UserController.queryRolesByRoleName
	 * @Description:easyui comgrid - 通过角色名称 查询角色信息
	 * @author: WenBin
	 * @date: 2017年10月17日
	 * @version: 1.0
	 * @param comgrid
	 * @return
	 * Object
	 * @update [日期YYYY-MM-DD] [更改人姓名][变更描述]
	 */
	// TODO
	@RequestMapping(value="/queryRolesByRoleName")
	@ResponseBody
	public Object queryRolesByRoleName(SearchComgrid comgrid){
		Page<UpRole> page = upRoleService.queryRolesByRoleName(comgrid);
		return new EasyuiPage<UpRole>(page.getContent(),page.getTotalElements(), page.getNumber()+1);
	}

	/**
	 * @Method: com.bozhon.web.base.controller.UserController.addPersonalRole
	 * @Description: 添加个人角色信息
	 * @author: WenBin
	 * @date: 2017年10月17日
	 * @version: 1.0
	 * @param userId
	 * @param roleId
	 * @return
	 * Object
	 * @throws Exception
	 * @update [日期YYYY-MM-DD] [更改人姓名][变更描述]
	 */
	// TODO
	@RequestMapping(value="/addPersonalRole")
	@ResponseBody
	public Object addPersonalRole(String userId,String roleId) throws Exception{
		if(StringUtils.isEmpty(userId)||StringUtils.isEmpty(roleId)){
			return new Msg(false,"50","角色ID 或者用户ID不存在！");
		}
		synchronized (this) {
			RelationUserRole relation = userRoleService.findOneByRoleIdAndUserId(roleId, userId);
			if(relation == null){
				relation = new RelationUserRole();
				relation.setRoleId(roleId);
				relation.setUserId(userId);
				userRoleService.save(relation);
			}else{
				return new Msg(false,"50","已经拥有该权限！");
			}
		}
		return new Msg();
	}

	/**
	 * @Method: com.bozhon.web.base.controller.UserController.delPersonalRole
	 * @Description: 删除个人角色权限
	 * @author: WenBin
	 * @date: 2017年10月18日
	 * @version: 1.0
	 * @param userId
	 * @param roleId
	 * @return
	 * Object
	 * @update [日期YYYY-MM-DD] [更改人姓名][变更描述]
	 */
	// TODO
	@RequestMapping(value="/delPersonalRole")
	@ResponseBody
	public Object delPersonalRole(String userId,String roleId){
		if(StringUtils.isEmpty(userId)||StringUtils.isEmpty(roleId)){
			return new Msg(false,"50","角色ID 或者用户ID不存在！");
		}
		synchronized (this) {
			/*			RelationUserRole relation = relationUserRoleService.findOneByRoleIdAndUserId(roleId, userId);
			if(relation != null){
				relationUserRoleService.delete(relation);
			}*/
		}
		return new Msg();
	}
}
